package com.wrs.examsystem.controller;


import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Topic;
import com.wrs.examsystem.service.TopicService;
import com.wrs.examsystem.util.wheel.TopicUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/examsystem/topic")
//@Api(tags = "题目相关的接口")
public class TopicController {

    @Resource
    TopicService topicService;


    /**
     * 查看题目详情
     * @param topicId 题目ID
     * @return
     */
    @ApiOperation("查看题目详情")
    @GetMapping("findDetail")
    public R findDetail(@RequestParam("topicId") Integer topicId){
        Topic topic = topicService.getById(topicId);

        topic.setTopicAnswer(null);

        //返回结果给前端
        return R.ok().data(topic);
    }

    /**
     * 更改题目
     * @param topic 题目
     * @return
     */
    @ApiOperation("修改题目")
    @PostMapping("updateTopic")
    public R updateTopic(@RequestBody Topic topic){

        //判断是否是老师或者管理员
        if(TopicUtil.judgeTopicEmpty(topic)){
            return R.error();
        }

        return topicService.updateTopic(topic);
        //返回结果给前端
    }

    /**
     * 添加题目
     * @param topic 题目
     * @return
     */
    @ApiOperation("添加题目")
    @PostMapping("insertTopic")
    public R insertTopic(@RequestBody Topic topic){
        if(TopicUtil.judgeTopicEmpty(topic)){
            return R.error();
        }
        return topicService.insertTopic(topic);
    }

    /**
     * 删除题目（逻辑删除）
     * @param topicId 题目ID
     * @return
     */
    @ApiOperation("删除题目")
    @DeleteMapping("deleteTopic")
    public R deleteTopic(@RequestParam("topicId") Integer topicId){
        return topicService.deleteTopicIdById(topicId);
    }
}

